#you can find the path of image by using following lines of code
import os
cwd = os.getcwd()
cwd
my_image = "lenna.png"
image_path = os.path.join(cwd , my_image)
image_path
'C:\\Users\\Dell\\lenna.png'
#Loading an image with filename
from PIL import Image
image = Image.open(my_image)
image
#loading an image using file path
image = Image.open(image_path)
image
#Plotting an Image
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.imshow(image)
<matplotlib.image.AxesImage at 0x2c48c262280>
#save image in different format
image.save("lenna.jpg")
#Gray Scale Images
from PIL import ImageOps
image_gray = ImageOps.grayscale(image)
image_gray
#Quantization(number of unique intensity values)
def get_concat_h(im1, im2):
dst = Image.new('RGB', (im1.width + im2.width, im1.height))
dst.paste(im1, (0, 0))
dst.paste(im2, (im1.width, 0))
return dst
for n in range(3,8):
plt.figure(figsize=(10,10))
plt.imshow(get_concat_h(image_gray, image_gray.quantize(256//2**n)))
plt.title("256 Quantization Levels left vs {} Quantization Levels right".format(256//2**n))
plt.show()
#color Channels
baboon =Image.open("baboon.png")
baboon
red, green, blue = baboon.split()
red, green, blue
(<PIL.Image.Image image mode=L size=512x512 at 0x2C48C6355E0>, <PIL.Image.Image image mode=L size=512x512 at 0x2C48C635EB0>, <PIL.Image.Image image mode=L size=512x512 at 0x2C48C635EE0>)
#Plotting the color image next to the red channel as a grayscale, we see that regions with red have higher intensity values.
get_concat_h(baboon, red)
#Plotting the color image next to the green channel as a grayscale
get_concat_h(baboon, green)
#Plotting the color image next to the blue channel as a grayscale
get_concat_h(baboon, blue)
# PIL Image to numpy array
# 'np.asarray' turns the original image to numpy array while 'np.array' makes a copy of image and then turns it into array
import numpy as np
array = np.array(image)
array
array([[[226, 137, 125],
[226, 137, 125],
[223, 137, 133],
...,
[230, 148, 122],
[221, 130, 110],
[200, 99, 90]],
[[226, 137, 125],
[226, 137, 125],
[223, 137, 133],
...,
[230, 148, 122],
[221, 130, 110],
[200, 99, 90]],
[[226, 137, 125],
[226, 137, 125],
[223, 137, 133],
...,
[230, 148, 122],
[221, 130, 110],
[200, 99, 90]],
...,
[[ 84, 18, 60],
[ 84, 18, 60],
[ 92, 27, 58],
...,
[173, 73, 84],
[172, 68, 76],
[177, 62, 79]],
[[ 82, 22, 57],
[ 82, 22, 57],
[ 96, 32, 62],
...,
[179, 70, 79],
[181, 71, 81],
[185, 74, 81]],
[[ 82, 22, 57],
[ 82, 22, 57],
[ 96, 32, 62],
...,
[179, 70, 79],
[181, 71, 81],
[185, 74, 81]]], dtype=uint8)
# The shape attribute of numpy gives (rows, columns, colors) of the image
array.shape
(512, 512, 3)
# Indexing
# you can plot array as an image
plt.figure(figsize=(10,10))
plt.imshow(array)
plt.show()
# We can use numpy slicing, for example, we can return the first 256 rows corresponding to the top half of the image:
rows = 256
plt.figure(figsize=(10,10))
plt.imshow(array[0:rows,:,:])
plt.show()
# We can also return the first 256 columns corresponding to the first half of the image.
columns = 256
plt.figure(figsize=(10,10))
plt.imshow(array[:,0:columns,:])
plt.show()
# We can plot the red channel as intensity values of the red channel.
baboon_array = np.array(baboon)
plt.figure(figsize=(10,10))
plt.imshow(baboon_array[:,:,0], cmap='gray')
plt.show()
# we can create a new array and set all but the red color channels to zero. Therefore, when we display the image it appears red:
baboon_red = baboon_array.copy()
baboon_red[:,:,1]=0
baboon_red[:,:,2]=0
plt.figure(figsize=(10,10))
plt.imshow(baboon_red)
plt.show()
# we can create a new array and set all but the blue color channels to zero. Therefore, when we display the image it appears blue:
baboon_blue = baboon_array.copy()
baboon_blue[:,:,1]=0
baboon_blue[:,:,0]=0
plt.figure(figsize=(10,10))
plt.imshow(baboon_blue)
plt.show()
# we can create a new array and set all but the green color channels to zero. Therefore, when we display the image it appears green:
baboon_green = baboon_array.copy()
baboon_green[:,:,2]=0
baboon_green[:,:,0]=0
plt.figure(figsize=(10,10))
plt.imshow(baboon_green)
plt.show()
# Use the image lenna.png or take any image.
# Open the image and create a PIL Image object called blue_lenna,
# convert the image into a numpy array we can manipulate called blue_array,
# get the blue channel out of it, and finally plot the image
from PIL import Image
blue_lenna = Image.open("lenna.png")
import numpy as np
import matplotlib.pyplot as plt
blue_array = np.array(blue_lenna)
blue_channel = blue_array.copy()
blue_channel[:,:,2]=0
plt.figure(figsize=(10,10))
plt.imshow(blue_channel)
plt.show()